home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / Stdio+ / stdio diffs next >
Text File  |  1989-10-16  |  14KB  |  627 lines

  1. Here are diffs to apply to LSC 3.0 stdio library sources to add the following
  2. functionality:
  3. *    %k and %lk formats for printf() (& Co.)
  4.     These formats print ints (%k) and longs (%lk) as characters, useful for ResType values.
  5. *    New 'device' ".SF" for fopen() and friends.
  6.     The 'device' ".SF" causes fopen() is prompt for a file using the Standard File get (read/append access) or put (write access) dialogs. This saves the user calling Standard File themselves, converting the volRef/name pair to a full pathname, and then calling fopen()...
  7. *    Added fdopen() and fdreopen() routines.
  8.     These mimic their Unix counterparts but take a volume reference as well as a name.
  9. *    Added in asm{...} statements.
  10.     Doesn't add functionality, just decreases size to offset increase caused by new code...
  11.  
  12. The diffs are for the files printf-3.c, stdopen.c and stdio.h.
  13.  
  14.                     Enjoy,
  15.                         Nigel (np@doc.ic.ac.uk)
  16.  
  17. ********************************    
  18. Filename:  printf-3.c, new file.
  19. --------------------------------
  20. Filename:  printf-3.c, original file.
  21.  
  22. 7    */
  23. 8    /*
  24. 9        Modified by Nigel Perry, May 1989
  25. 10        
  26. 11        Added %k and %lk format to print word/longs as characters
  27. 12        Code controlled by #ifdef CHARINT
  28. 13        
  29. 14        © 1989, Nigel Perry
  30. 15     */
  31. 16     
  32. 17    /* add in %k format */
  33. 18    #define CHARINT
  34. 19    
  35. --------------------------------
  36. 7    */
  37. 8    
  38.  
  39. 22    
  40. 23    #ifndef _stdioh_
  41. --------------------------------
  42. 11    
  43. 12    #include "config.h"
  44. 13    
  45. 14    #ifndef _stdioh_
  46.  
  47. 287    {
  48. 288    Boolean left_justify;           /* flag to indicate left justification */
  49. 289    Boolean do_precision;           /* flag limited precision */
  50. --------------------------------
  51. 278    {
  52. 279    Boolean left_justify;           /* flag to indicate left justiccation */
  53. 280    Boolean do_precision;           /* flag limited precision */
  54.  
  55. 635                        break;
  56. 636    #ifdef CHARINT
  57. 637            case 'k':
  58. 638                {    register char *arg_hold;
  59. 639    
  60. 640                    length = its_a_long ? sizeof(long) : sizeof(int);
  61. 641                    arg_hold = argument + length;
  62. 642                    if (do_precision && length > precision) length = precision;
  63. 643                    width -= length;
  64. 644                    
  65. 645                    if(!left_justify) padd(BLANKS, width);
  66. 646                    while (--length >= 0)
  67. 647                        (*outputx)(*argument++);
  68. 648                    if(left_justify) padd(BLANKS, width);
  69. 649    
  70. 650                    argument = arg_hold;
  71. 651                }
  72. 652                break;
  73. 653    #endif
  74. 654    
  75. --------------------------------
  76. 626                        break;
  77. 627    
  78.  
  79.  
  80. 4 mismatches.
  81.  
  82. ********************************    
  83. Filename:  stdopen.c, new file.
  84. --------------------------------
  85. Filename:  stdopen.c, original file.
  86.  
  87. 5    */
  88. 6    /*
  89. 7        Modified by Nigel Perry, May 1989
  90. 8        
  91. 9        Added new "driver" name ".SF" which uses the Standard File Package
  92. 10        to select a file.
  93. 11        
  94. 12        All .SF code #ifdef'ed by STDFILE
  95. 13        
  96. 14        Added fdopen() and fdreopen() routines to open files given
  97. 15        volume reference and name.
  98. 16        
  99. 17        All fdopen() code #ifdef'ed by FDOPEN
  100. 18        
  101. 19        Added inline traps. All asm code #ifdef'ed by USEASM
  102. 20        
  103. 21        © 1989, Nigel Perry
  104. 22     */
  105. 23     
  106. 24    /* if defined will include new ".SF" "driver" */
  107. 25    #define STDFILE
  108. 26    /* if defined will include fdopen() code */
  109. 27    #define FDOPEN
  110. 28    /* do trap calls inline */
  111. 29    #define USEASM
  112. 30    
  113. 31    #ifndef _stdioh_
  114. --------------------------------
  115. 5    */
  116. 6     
  117. 7    #ifndef _stdioh_
  118.  
  119. 42    
  120. 43    #ifdef USEASM
  121. 44    #include <asm.h>
  122. 45    #endif
  123. 46    
  124. 47    #ifdef STDFILE
  125. 48    #ifndef    _StdFilePkg_
  126. 49    #include <StdFilePkg.h>
  127. 50    #endif
  128. 51    
  129. 52    /* "Point" to place dialog */
  130. 53    #define GETWHERE 0x00470052L
  131. 54    #define PUTWHERE 0x004f0068L
  132. 55    #define NIL (void *)0
  133. 56    #define EMPTY ((char *)0xA02)        /* low memory global OneOne */
  134. 57                                        /* used for "" and "\p" */
  135. 58    
  136. 59    #endif
  137. 60    
  138. 61    static Boolean _exiting_closeall_flag = false;
  139. --------------------------------
  140. 18    
  141. 19    static Boolean _exiting_closeall_flag = false;
  142.  
  143. 66    #line 0 xfopen
  144. 67    #ifdef FDOPEN
  145. 68    static FILE    *xfopen(volref,nameptr,type,who)
  146. 69    int volref;
  147. 70    #else
  148. 71    static FILE    *xfopen(nameptr,type,who)
  149. 72    #endif
  150. 73    char    *nameptr;
  151. --------------------------------
  152. 24    #line 0 xfopen
  153. 25    static FILE    *xfopen(nameptr,type,who)
  154. 26    char    *nameptr;
  155.  
  156. 89        register Ptr    p;
  157. 90    #ifdef STDFILE
  158. 91        SFReply reply;
  159. 92        SFTypeList types;
  160. 93        Boolean    SFdriver = false;
  161. 94    #endif
  162. 95    #ifdef FDOPEN
  163. 96        Boolean FDopen = false;
  164. 97    #endif
  165. 98    
  166. --------------------------------
  167. 42        register Ptr    p;
  168. 43    
  169.  
  170. 157    
  171. 158    #ifndef STDFILE
  172. 159    #ifdef FDOPEN
  173. 160        if(volref != 0)
  174. 161            FDopen = true;
  175. 162        else
  176. 163    #endif
  177. 164            CtoPstr(nameptr);
  178. 165    #else
  179. 166    #ifdef FDOPEN
  180. 167        if(volref != 0)
  181. 168            FDopen = true;
  182. 169        else
  183. 170    #endif
  184. 171        /* I would use *((long *)nameptr) == '.SF\0' if it were aligned... */
  185. 172        if( nameptr[0] == '.' && nameptr[1] == 'S'
  186. 173         && nameptr[2] == 'F' && nameptr[3] == '\0' )
  187. 174        {    SFdriver = true;
  188. 175            if(!create || add)
  189. 176                SFGetFile(GETWHERE, EMPTY, NIL, -1, types, NIL, &reply);
  190. 177            else
  191. 178                SFPutFile(PUTWHERE, EMPTY, EMPTY, NIL, &reply);
  192. 179            if(!reply.good)
  193. 180            {    errno = bdNamErr;
  194. 181                return(NULL);
  195. 182            }
  196. 183        }
  197. 184        else
  198. 185            CtoPstr(nameptr);
  199. 186    #endif
  200. 187    
  201. 188    #ifndef USEASM
  202. 189        p = NewPtr((long)BUFSIZ);
  203. 190    #else
  204. 191        asm
  205. 192        {    move.l        #BUFSIZ,d0
  206. 193            _NewPtr
  207. 194            movea.l        a0,p
  208. 195        }
  209. 196    #endif
  210. 197    
  211. 198        do    {
  212. 199    
  213. 200    #ifdef STDFILE
  214. 201            if(!SFdriver)
  215. 202            {
  216. 203    #endif
  217. 204    #ifdef FDOPEN
  218. 205                if(!FDopen)
  219. 206                {
  220. 207    #endif    
  221. 208                    /* pdg 7/29/86 - look only via default path */
  222. 209                    pb.ioNamePtr    = 0;
  223. 210    #ifndef USEASM
  224. 211                    if (PBGetVol(&pb, false)) pb.ioVRefNum    = 0;
  225. 212    #else
  226. 213                    asm
  227. 214                    {    lea        pb,a0
  228. 215                        _PBGetVol
  229. 216                        beq.s    @2
  230. 217                        clr.w    pb.ioVRefNum
  231. 218                    @2
  232. 219                    }
  233. 220    #endif
  234. 221    #ifdef FDOPEN
  235. 222                }
  236. 223                else
  237. 224                    pb.ioVRefNum = volref;
  238. 225    #endif    
  239. 226                pb.ioNamePtr    = (StringPtr)nameptr;
  240. 227    #ifdef STDFILE
  241. 228            }
  242. 229            else
  243. 230            {    pb.ioNamePtr = (StringPtr)&reply.fName;
  244. 231                pb.ioVRefNum = reply.vRefNum;
  245. 232            }
  246. 233    #endif
  247. 234            pb.ioVersNum    = 0;
  248. --------------------------------
  249. 102    
  250. 103        CtoPstr(nameptr);
  251. 104    
  252. 105        p = NewPtr((long)BUFSIZ);
  253. 106    
  254. 107        do    {
  255. 108        
  256. 109            /* pdg 7/29/86 - look only via default path */
  257. 110    
  258. 111            pb.ioNamePtr    = 0;
  259. 112            if (PBGetVol(&pb, false)) pb.ioVRefNum    = 0;
  260. 113    
  261. 114    
  262. 115            pb.ioNamePtr    = (StringPtr)nameptr;
  263. 116            pb.ioVersNum    = 0;
  264.  
  265. 237    
  266. 238    #ifndef USEASM
  267. 239            new = PBOpen(&pb,false);
  268. 240    #else
  269. 241            asm
  270. 242            {    lea        pb,a0
  271. 243                _PBOpen
  272. 244                move.w    d0,new
  273. 245            }
  274. 246    #endif
  275. 247    
  276. 248            if ((new == fnfErr) && (create))
  277. 249                {
  278. 250    #ifndef USEASM
  279. 251                if (err = (PBCreate(&pb,false))) goto err_exit;
  280. 252    #else
  281. 253                asm
  282. 254                {    lea        pb,a0
  283. 255                    _PBCreate
  284. 256                    bne        @err_exit
  285. 257                }
  286. 258    #endif
  287. 259    
  288. --------------------------------
  289. 119    
  290. 120            new = PBOpen(&pb,false);
  291. 121            
  292. 122            if ((new == fnfErr) && (create))
  293. 123                {
  294. 124                if (err = (PBCreate(&pb,false))) goto err_exit;
  295. 125    
  296.  
  297. 263                pb2.ioFVersNum    = pb.ioVersNum;
  298. 264    #ifndef USEASM
  299. 265                if (err = (PBGetFInfo(&pb2,false))) goto err_exit;
  300. 266    #else
  301. 267                asm
  302. 268                {    lea        pb2,a0
  303. 269                    _PBGetFInfo
  304. 270                    bne        @err_exit
  305. 271                }
  306. 272    #endif
  307. 273    
  308. 274                pb2.ioFlFndrInfo.fdType        = 'TEXT';
  309. 275                pb2.ioFlFndrInfo.fdCreator    = '????';
  310. 276    #ifndef USEASM
  311. 277                if (err = (PBSetFInfo(&pb2,false))) goto err_exit;
  312. 278    #else
  313. 279                asm
  314. 280                {    lea        pb2,a0
  315. 281                    _PBSetFInfo
  316. 282                    bne        @err_exit
  317. 283                }
  318. 284    #endif
  319. 285    
  320. --------------------------------
  321. 129                pb2.ioFVersNum    = pb.ioVersNum;
  322. 130                if (err = (PBGetFInfo(&pb2,false))) goto err_exit;
  323. 131    
  324. 132                pb2.ioFlFndrInfo.fdType        = 'TEXT';
  325. 133                pb2.ioFlFndrInfo.fdCreator    = '????';
  326. 134                if (err = (PBSetFInfo(&pb2,false))) goto err_exit;
  327. 135    
  328.  
  329. 295                    {
  330. 296    #ifndef USEASM
  331. 297                        if(err = (PBGetEOF(&pb,false))) goto err_exit;
  332. 298    #else
  333. 299                        asm
  334. 300                        {    lea        pb,a0
  335. 301                            _PBGetEOF
  336. 302                            bne        @err_exit
  337. 303                        }
  338. 304    #endif
  339. 305        
  340. 306                        pb.ioPosOffset = (long)pb.ioMisc;
  341. 307                        pb.ioPosMode = fsFromStart;
  342. 308    #ifndef USEASM
  343. 309                        err = PBSetFPos(&pb,false);
  344. 310                        if ((err!=noErr) && (err!=eofErr)) goto err_exit;
  345. 311    #else
  346. 312                        asm
  347. 313                        {    lea        pb,a0
  348. 314                            _PBSetFPos
  349. 315                            beq.s    @1
  350. 316                            cmp.w    #eofErr,d0
  351. 317                            bne        @err_exit
  352. 318                        @1
  353. 319                        }
  354. 320    #endif
  355. 321                    }
  356. --------------------------------
  357. 145                    {
  358. 146                        if(err = (PBGetEOF(&pb,false))) goto err_exit;
  359. 147        
  360. 148                        pb.ioPosOffset = (long)pb.ioMisc;
  361. 149                        pb.ioPosMode = fsFromStart;
  362. 150                        err = PBSetFPos(&pb,false);
  363. 151                        if ((err!=noErr) && (err!=eofErr)) goto err_exit;
  364. 152                    }
  365.  
  366. 328                        pb2.ioFVersNum    = pb.ioVersNum;
  367. 329    #ifndef USEASM
  368. 330                        if (err = (PBGetFInfo(&pb2,false))) goto err_exit;
  369. --------------------------------
  370. 159                        pb2.ioFVersNum    = pb.ioVersNum;
  371. 160                        if (err = (PBGetFInfo(&pb2,false))) goto err_exit;
  372.  
  373. 340                        new = PBOpen(&pb,false);
  374. 341    #else
  375. 342                        asm
  376. 343                        {    lea        pb2,a0
  377. 344                            _PBGetFInfo
  378. 345                            bne        @err_exit
  379. 346                            lea        pb,a0
  380. 347                            _PBClose
  381. 348                            bne        @err_exit
  382. 349                            lea        pb,a0
  383. 350                            _PBDelete
  384. 351                            bne        @err_exit
  385. 352                            lea        pb,a0
  386. 353                            _PBCreate
  387. 354                            bne        @err_exit
  388. 355                            lea        pb2,a0
  389. 356                            _PBSetFInfo
  390. 357                            bne        @err_exit
  391. 358                            lea        pb,a0
  392. 359                            _PBOpen
  393. 360                            move.l    d0,new
  394. 361                        }
  395. 362    #endif
  396. 363                        create = false;
  397. --------------------------------
  398. 170                        new = PBOpen(&pb,false);
  399. 171                        create = false;
  400.  
  401. 387                    who->inbuf        = 0;
  402. 388    #ifdef STDFILE
  403. 389    #ifdef FDOPEN
  404. 390                    if(!SFdriver && !FDopen)
  405. 391    #else
  406. 392                    if(!SFDriver)
  407. 393    #endif
  408. 394    #else
  409. 395    #ifdef FDOPEN
  410. 396                    if(!FDopen)
  411. 397    #endif
  412. 398    #endif
  413. 399                        PtoCstr(nameptr);
  414. 400                    if (_init_onexit == false)
  415. --------------------------------
  416. 195                    who->inbuf        = 0;
  417. 196                    PtoCstr(nameptr);
  418. 197                    if (_init_onexit == false)
  419.  
  420. 425    err_exit:
  421. 426    #ifndef USEASM
  422. 427        errno = err;
  423. 428    #else
  424. 429        asm
  425. 430        {    move.l        d0,errno
  426. 431        }
  427. 432    #endif
  428. 433        
  429. 434    exit_noset:
  430. 435    
  431. 436    #ifndef USEASM
  432. 437        DisposPtr(p);
  433. 438    #else
  434. 439        asm
  435. 440        {    movea.l        p,a0
  436. 441            _DisposPtr
  437. 442        }
  438. 443    #endif
  439. 444    
  440. 445    #ifdef STDFILE
  441. 446    #ifdef FDOPEN
  442. 447        if(!SFdriver && !FDopen)
  443. 448    #else
  444. 449        if(!SFdriver)
  445. 450    #endif
  446. 451    #else
  447. 452    #ifdef FDOPEN
  448. 453        if(!FDopen)
  449. 454    #endif
  450. 455    #endif
  451. 456            PtoCstr(nameptr);
  452. 457        return(NULL);
  453. --------------------------------
  454. 222    err_exit:
  455. 223        errno = err;
  456. 224        
  457. 225    exit_noset:
  458. 226    
  459. 227        DisposPtr(p);
  460. 228        PtoCstr(nameptr);
  461. 229        return(NULL);
  462.  
  463. 498                        goto err2_exit;
  464. 499                pb.ioRefNum = refnum;
  465. --------------------------------
  466. 270                        goto err2_exit;
  467. 271    
  468. 272                pb.ioRefNum = refnum;
  469.  
  470. 507    
  471. 508    #ifndef USEASM
  472. 509                    if (err = who->last_error = PBWrite(&pb,false))
  473. 510                        errno = err;
  474. 511    #else
  475. 512                    asm
  476. 513                    {    lea            pb,a0
  477. 514                        _PBWrite
  478. 515                        move.w        d0,err
  479. 516                        move.w        d0,OFFSET(FILE,last_error)(who)
  480. 517                        beq.s        @3
  481. 518                        move.w        d0,errno
  482. 519                    @3
  483. 520                    }
  484. 521    #endif
  485. 522                }
  486. 523    
  487. 524                who->InUse = false;
  488. 525    #ifndef USEASM
  489. 526                if (err = (PBClose(&pb,false)))        /* close file */
  490. 527                    goto err2_exit;
  491. 528    #else
  492. 529                asm
  493. 530                {    lea        pb,a0
  494. 531                    _PBClose
  495. 532                    move.w    d0,err
  496. 533                    bne        @err2_exit
  497. 534                }
  498. 535    #endif
  499. 536    
  500. --------------------------------
  501. 280    
  502. 281                    if (err = who->last_error = PBWrite(&pb,false))
  503. 282                        errno = err;
  504. 283                }
  505. 284    
  506. 285                who->InUse = false;
  507. 286                if (err = (PBClose(&pb,false)))        /* close file */
  508. 287                    goto err2_exit;
  509. 288    
  510.  
  511. 540                    pb.ioNamePtr = 0L;
  512. 541    #ifndef USEASM
  513. 542                    if (err = (PBFlshVol(&pb,false)))        /* flush vol */
  514. 543                        goto err2_exit;
  515. 544    #else
  516. 545                    asm
  517. 546                    {    lea        pb,a0
  518. 547                        _PBFlshVol
  519. 548                        move.w    d0,err
  520. 549                        bne        @err2_exit
  521. 550                    }
  522. 551    #endif
  523. 552                }
  524. --------------------------------
  525. 292                    pb.ioNamePtr = 0L;
  526. 293                    if (err = (PBFlshVol(&pb,false)))        /* flush vol */
  527. 294                        goto err2_exit;
  528. 295                }
  529.  
  530. 569            if (!who->user_buf)
  531. 570    #ifndef USEASM
  532. 571                DisposPtr(who->filebuf);            /* dispose mem*/
  533. 572    #else
  534. 573                asm
  535. 574                {    movea.l        OFFSET(FILE,filebuf)(who),a0
  536. 575                    _DisposPtr
  537. 576                }
  538. 577    #endif
  539. 578            return(0);
  540. --------------------------------
  541. 312            if (!who->user_buf)
  542. 313                DisposPtr(who->filebuf);            /* dispose mem*/
  543. 314    
  544. 315            return(0);
  545.  
  546. 597    {
  547. 598    #ifdef FDOPEN
  548. 599        return(xfopen(0,nameptr,type,NULL));
  549. 600    #else
  550. 601        return(xfopen(nameptr,type,NULL));
  551. 602    #endif
  552. 603        
  553. --------------------------------
  554. 334    {
  555. 335        return(xfopen(nameptr,type,NULL));
  556. 336        
  557.  
  558. 616        if(fclose(who)) return(NULL);
  559. 617    #ifdef FDOPEN
  560. 618        return(xfopen(0,nameptr,type,who));
  561. 619    #else
  562. 620        return(xfopen(nameptr,type,who));
  563. 621    #endif
  564. 622    }
  565. 623    
  566. 624    #ifdef FDOPEN
  567. 625    #line 0 fdopen
  568. 626    FILE    *fdopen(volref,nameptr,type)
  569. 627        int        volref;
  570. 628        char    *nameptr,*type;
  571. 629    {
  572. 630        return(xfopen(volref,nameptr,type,NULL));
  573. 631    
  574. 632    }
  575. 633    
  576. 634    #line 0 fdreopen
  577. 635    FILE    *fdreopen(volref,nameptr,type,who)
  578. 636        int        volref;
  579. 637        char    *nameptr,*type;
  580. 638        FILE    *who;
  581. 639    
  582. 640    {
  583. 641        if(fclose(who)) return(NULL);
  584. 642        return(xfopen(volref,nameptr,type,who));
  585. 643    }
  586. 644    #endif
  587. 645    
  588. --------------------------------
  589. 349        if(fclose(who)) return(NULL);
  590. 350        return(xfopen(nameptr,type,who));
  591. 351    }
  592. 352    
  593. 353    
  594.  
  595. 18 mismatches.
  596.  
  597. ********************************    
  598. Filename:  stdio.h, new file.
  599. --------------------------------
  600. Filename:  stdio.h, original file.
  601.  
  602. 7     * (C) Copyright 1985, 1986. THINK Technologies Inc. All rights reserved.
  603. 8     *
  604. 9     * Added in fdopen() & fdreopen(), Nigel Perry, 1989
  605. 10     */
  606. --------------------------------
  607. 7     * (C) Copyright 1985, 1986. THINK Technologies Inc. All rights reserved.
  608. 8     */
  609.  
  610. 72    FILE    *freopen();        /* open a file using the given stream */
  611. 73    FILE    *fdopen();        /* open a file using volref/name pair */
  612. 74    FILE    *fdreopen();    /* open a file using volref/name pair on the given stream */
  613. 75    extern    FILE    _file[_NFILE];    /* file descriptor array (internal) */
  614. 76    extern    FILE    _console_;        /* device name for the console */
  615. 77    long    ftell();        /* get position within file */
  616. 78    
  617. --------------------------------
  618. 70    FILE    *freopen();        /* open a file using the given stream */
  619. 71    extern    FILE    _file[_NFILE];    /* file descriptor array (internal) */
  620. 72    extern    FILE    _console_;        /* device name for the console */
  621. 73    long    ftell();        /* get position withen file */
  622. 74    
  623.  
  624.  
  625. 2 mismatches.
  626.  
  627.